package sort1;

public class Sort {

    /**
     * 时间复杂度：O(N)
     * 空间复杂度：O(1)
     * 稳定性：稳定
     * @param arr
     */
    public static void insertSort(int[] arr){
        for(int i=1;i< arr.length;i++){
            int j=i-1;
            int tmp = arr[i];
            while (j>=0){
                if(arr[j]>tmp){
                    arr[j+1]=arr[j];
                }else {
                    arr[j+1]=tmp;
                    break;
                }
                j--;
            }
            arr[j+1]=tmp;
        }
    }

    public static void shellSort(int[] arr){
        int gap = arr.length;
        while (gap>1){
            gap/=2;
            shell(arr,gap);
        }
    }

    private static void shell(int[] arr, int gap) {
        for (int i = gap; i <arr.length; i++) {
            int tmp = arr[i];
            int j=i-gap;
            for (;j>=0;j-=gap){
                if (arr[j]>tmp) {
                    arr[j+gap] = arr[j];
                } else {
                    arr[j+gap] = tmp;
                    break;
                }
            }
            arr[j+gap]=tmp;
        }
    }

    public static void selectSort(int[] arr){
        for (int i=0;i<arr.length;i++){
            int midIndex=i;
            for(int j=i+1;j < arr.length;j++){
                if (arr[j]<arr[midIndex]){
                    midIndex=j;
                }
            }
            swap(arr,i,midIndex);
        }
    }

    public static void selectSort2(int[] arr){
        int left = 0;
        int right = arr.length-1;
        while (left<right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left+1; i <= right; i++) {
                if (arr[i]<arr[minIndex]){
                    minIndex=i;
                }
                if (arr[i]>arr[maxIndex]){
                    maxIndex=i;
                }
            }
            swap(arr,left,minIndex);
            if (maxIndex==left){
                maxIndex=minIndex;
            }
            swap(arr,right,maxIndex);
            left++;
            right--;
        }
    }

    public static void heapSort(int[] arr){
        //创建大根堆
        createHeap(arr);
        int end = arr.length-1;
        while (end>0){
            swap(arr,0,end);
            shiftDown(arr,0,end);
            end--;
        }
    }

    public static void bubbleSort(int[] arr){
        for (int i=0;i< arr.length-1;i++){
            boolean flag = true;
            for (int j=0;j< arr.length-1-i;j++){
                if (arr[j+1]<arr[j]){
                    swap(arr,j,j+1);
                    flag=false;
                }
            }
            if (flag){
                break;
            }
        }
    }

    public static void quickSort(int[] arr) {
        quick(arr,0,arr.length-1);
    }

    private static void quick(int[] arr, int left, int right) {
        if (left>=right){
            return;
        }
        int pivot = partition(arr,left,right);
        quick(arr,left,pivot-1);
        quick(arr,pivot+1,right);
    }

    public static int partition(int[] arr,int left,int right){
        int tmp=arr[left];
        int prev=left;
        int cur=left+1;
        while (cur<=right){
            if (arr[cur]<tmp&&arr[++prev]!=arr[cur]){
                swap(arr,prev,cur);
            }
            cur++;
        }
        swap(arr,left,prev);
        return prev;
    }

    private static int partition1(int[] arr, int left, int right) {
        int tmp = arr[left];
        int tmpLeft=left;
        while (left<right){
            while (left < right && arr[right]>=tmp){
                right--;
            }
            while (left < right && arr[left]<=tmp){
                left++;
            }
            swap(arr,left,right);
        }
        swap(arr,left,tmpLeft);
        return left;
    }

    private static void createHeap(int[] arr) {
        int usedSize = arr.length;
        for (int parent = (usedSize-1-1)/2;parent>=0;parent--){
            shiftDown(arr,parent,usedSize);
        }
    }

    private static void shiftDown(int[] arr,int parent, int usedSize) {
        int child = parent*2+1;
        while (child<usedSize){
            //找出左右孩子的最大值
            if (child+1<usedSize&&arr[child+1]>arr[child]){
                child=child+1;
            }
            if (arr[child]>arr[parent]){
                swap(arr,parent,child);
                parent=child;
                child=parent*2+1;
            }else {
                break;
            }
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp=arr[i];
        arr[i]=arr[j];
        arr[j]=tmp;
    }


//    public static void insertSort(int[] arr){
//        for (int i = 1; i < arr.length; i++) {
//            int tmp = arr[i];
//            int j=i-1;
//            for (;j>=0;j--){
//                if (arr[j]>arr[j+1]){
//                    arr[j+1]=arr[j];
//                }else {
//                    arr[j+1]=tmp;
//                    break;
//                }
//            }
//            arr[j+1]=tmp;
//        }
//    }

}
